#!/bin/bash
# echo "=======================================1. 配置网络 "
# echo
# echo "1.1 请输入 IPADDR ："
# read IPADDR

# #echo "1.2 请输入 NETMASK 子网掩码："
# #read NETMASK
# NETMASK="255.255.255.0"
# echo "1.3 请输入 GATEWAY 网关："
# read GATEWAY 

# #echo "1.4 请输入 DNS："
# #read DNS 

# DNS="8.8.8.8"

# echo "1.5 写入 /etc/sysconfig/network-scripts/"
# cat > /etc/sysconfig/network-scripts/ifcfg-ens33 << EOF
# TYPE=Ethernet
# PROXY_METHOD=none
# BROWSER_ONLY=no
# BOOTPROTO=static
# DEFROUTE=yes
# IPV4_FAILURE_FATAL=no
# IPV6INIT=yes
# IPV6_AUTOCONF=yes
# IPV6_DEFROUTE=yes
# IPV6_FAILURE_FATAL=no
# IPV6_ADDR_GEN_MODE=stable-privacy
# NAME=ens33
# UUID=036bcbf5-d8ac-44ec-87f4-485d540c5d3b
# DEVICE=ens33
# ONBOOT=yes
# IPADDR=$IPADDR
# NETMASK=$net
# GATEWAY=$GATEWAY
# DNS1=$DNS
# EOF

IPADDR="192.168.100.252" # ip
NETMASK="255.255.255.0"# 子网掩码
GATEWAY="192.168.100.1"# 网关
DNS="8.8.8.8"

echo "======================================= 2 关闭防火墙、swap"
systemctl stop firewalld && systemctl disable firewalld && sed -ri 's/.*swap.*/#&/' /etc/fstab 


echo "======================================= 3 配置节点名称，以及hosts文件"
echo "3.1 请输入节点名称:"
read name
echo "3.2 请输入主节点ip:"
read master_ip
hostnamectl set-hostname $name
echo "3.3 请输入hosts 配置:"
read hosts
cat >> /etc/hosts << EOF
$hosts
EOF

echo "======================================= 4. 安装docker "
echo "4.1 安装docker"
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo && yum -y install docker-ce-18.06.1.ce-3.el7
systemctl enable docker && systemctl start docker
echo "4.2 设置docker配置文件"
cat > /etc/docker/daemon.json << EOF
{
  	"registry-mirrors": ["https://nz3z0zzn.mirror.aliyuncs.com"],
	"insecure-registries": ["$master_ip:5000"],
	"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
echo "4.2.1 解决bug"
systemctl daemon-reload && systemctl restart docker && echo "1" >  /proc/sys/net/bridge/bridge-nf-call-iptables

if [ $name == "master" ]
then
echo "4.3 私有镜像库"
docker pull registry && docker run -itd -p 5000:5000 --restart always -v /opt/data/registry/:/var/lib/registry --name registry registry 	
 
fi


echo "=======================================5. 安装 kubernetes"
echo"5.1 配置添加阿里云YUM软件源"
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
echo "5.2 安装k8s \设置开机自启动"
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0 && systemctl enable kubelet

echo "2.3 安装 cni 网络插件"
kubectl apply -f https://gitee.com/dev-linhu/local-k8s/raw/master/kube-flannel.yml
kubectl get pods -n kube-system

systemctl stop docker
kubeadm reset -f
systemctl start docker


if [ $name == "master" ]
then
echo "5.3 解决 镜像拉取失败 registry.k8s.io 当前只针对 1.28.2版本生效 请自行更改 、  Initial timeout of 40s passed."
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.28.2
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.28.2 registry.k8s.io/kube-apiserver:v1.28.2

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.28.2
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.28.2 registry.k8s.io/kube-controller-manager:v1.28.2

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.28.2
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.28.2 registry.k8s.io/kube-scheduler:v1.28.2

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.28.2
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.28.2 registry.k8s.io/kube-proxy:v1.28.2

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.9
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.9 registry.k8s.io/pause:3.9

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.5.9-0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.5.9-0 registry.k8s.io/etcd:3.5.9-0

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:v1.10.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:v1.10.1 registry.k8s.io/coredns/coredns:v1.10.1
echo "5.3 master节点kubectl init 初始化"

kubeadm init \
  --apiserver-advertise-address=$master_ip \
  --image-repository registry.aliyuncs.com/google_containers \
  --kubernetes-version v1.18.0 \
  --service-cidr=10.96.0.0/12 \
  --pod-network-cidr=10.244.0.0/16
echo "请保存 kubeadm join ... 子节点运行 "
mkdir -p $HOME/.kube && sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config &&  sudo chown $(id -u):$(id -g) $HOME/.kube/config

else
kubeadm reset -f
echo "请复制 /etc/kubernetes/admin.conf 到子节点后点击回车进行下一步:"
read next
export KUBECONFIG=/etc/kubernetes/admin.conf
kubectl get node
fi


exit

